package test.designmode.builder;

import test.bean.User;

import java.util.Date;

/**
 * 本例子参考JpushSDK的PushPayload
 */
public class Game {
    private String id;
    private User user;
    private int amount;
    private Date createTime;

    private Game(String id, User user, int amount, Date createTime) {
        this.id = id;
        this.user = user;
        this.amount = amount;
        this.createTime = createTime;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    @Override
    public String toString() {
        return "Game{" +
                "id='" + id + '\'' +
                ", user=" + user +
                ", amount=" + amount +
                ", createTime=" + createTime +
                '}';
    }

    public static class Builder {
        private String id;
        private User user = null;
        private int amount;
        private Date createTime = null;

        public Builder setId(String id) {
            this.id = id;
            return this;
        }

        public Builder setUser(User user) {
            this.user = user;
            return this;
        }

        public Builder setAmount(int amount) {
            this.amount = amount;
            return this;
        }

        public Builder setCreateTime(Date createTime) {
            this.createTime = createTime;
            return this;
        }

        /**
         * builder模式的核心优势在于这个build方法, 可以对参数进行统一检查
         *
         * @return
         */
        public Game build() {
            if (id == null || user == null) throw new RuntimeException("id and user both should be set");
            return new Game(id, user, amount, createTime);
        }
    }

}
